<template>
  <div>
    <top-list-admin :activeIndex="2+''"></top-list-admin>
    <div style=" display: flex; align-items: center; justify-content: space-between;">
      <el-button plain type="primary" @click="userEdit(0)" style="margin-left: 20px;margin-top: 20px">添加账户</el-button>
      <div style="display: flex;  justify-content: space-between;margin-right: 10px">
        <el-input v-model="listQuery.userNum" placeholder="登入用户名" style="width: 200px;" class="filter-item" clearable @keyup.enter.native="handleFilter"/>
        <el-input v-model="listQuery.userName" placeholder="姓名" style="width: 200px;" class="filter-item" clearable @keyup.enter.native="handleFilter" />
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
          Search
        </el-button>
      </div>
    </div>
    <el-table
      :data="dataList"
      border
      stripe
      v-loading="dataListLoading"
      style="width: 100%; margin-top: 20px;">
      <el-table-column prop="userNum" header-align="center" align="center" label="登入用户名"></el-table-column>
      <el-table-column prop="userName" header-align="center" align="center" label="姓名"></el-table-column>
      <el-table-column prop="userPhone" header-align="center" align="center" label="电话"></el-table-column>
      <el-table-column prop="userEmail" header-align="center" align="center" label="邮箱"></el-table-column>
      <el-table-column prop="userPost" header-align="center" align="center" label="职务"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="250" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="userEdit(scope.row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="lookHandle(scope.row.userNum)">设置权限</el-button>
          <el-button type="text" size="small" @click="resetSecret(scope.row.userNum)">重置密码</el-button>
          <el-button type="text" size="small" @click="userDelete(scope.row.id,scope.row.userNum)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top: 20px;"
    >
    </el-pagination>
    <user-update v-if="updateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></user-update>
  </div>
</template>

<script>
import user from '../../api/user'
import myUtils from '../../api/myUtils'
import TopListAdmin from '@/components/TopListAdmin'
import userUpdate from './user-update.vue'
import site from '../../api/site'
export default {
  components: {
    userUpdate,
    TopListAdmin,
  },
  data() {
    return {
      dataList: [
        {
          id:0,
        },
      ],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      updateVisible: false,
      search: null,
      listQuery: {}
    }
  },
  created() {
    this.isadmin()
  },
  methods: {
    isadmin()
    {
      let role = myUtils.getRole(this.$route.query.subjectPkId,this.$store.getters.roles,this.$store.getters.isSystemAdministrator);
      if(role !== 8){
        this.$message.error("您不是系统管理员");
      }
      else {
        this.getDataList();
      }
    },

    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },

    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },

    getDataList()
    {
      this.dataListLoading = true;
      user.userSearch(this.pageIndex,this.pageSize,this.listQuery.userNum,this.listQuery.userName).then(response=>{
        this.dataList = response.data.records;
        this.totalPage = response.data.total;
      });
      this.dataListLoading = false;
      // research.getByResearchByUser(this.$store.getters.name,this.pageIndex,this.pageSize).then(response=>{
      //   this.dataList = response.data.records;
      //   this.totalPage = response.data.total;
      //
      // });
      //@发现一个问题在then里面的datalist和在外面的datalist不一样 有可能是更新完列表就消失了
    },

    handleFilter() {
      if (this.listQuery.userNum === '') {
        this.listQuery.userNum = null
      }
      if (this.listQuery.userName === '') {
        this.listQuery.userName = null
      }
      this.getDataList();
    },

    lookHandle(userNum)
    {
      // this.$store.commit('set_subjectPkId',subjectPkId)
      user.getUserVoByUserNum(userNum).then((response) => {
        console.log(response)
      })
      this.$router.push({path:`/upload/userInfo?userNum=${userNum}`})
    },

    resetSecret(UserNum)
    {
      user.resetPassWord(UserNum).then(({ code }) => {
        if (code  === '000000') {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 700,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },

    userEdit(id)
    {
      console.log(id)
      let role =myUtils.getRole(this.$route.query.subjectPkId,this.$store.getters.roles,this.$store.getters.isSystemAdministrator);
      if(role !== 8) //总管理员才能操作
      {
        this.$message.error("您不是系统管理员");
        return;
      }
      this.updateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
      console.log(this.updateVisible)
    },

    userDelete(id,name)
    {
      let role =myUtils.getRole(this.$route.query.subjectPkId,this.$store.getters.roles,this.$store.getters.isSystemAdministrator);
      if(role !== 8) //总管理员才能操作
      {
        this.$message.error("您不是系统管理员");
        return;
      }
      this.$confirm(
        `确定对[账户名为：${name}]进行删除操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(()=>{
        user.deleteUser(id).then(( response ) => {
          console.log(response)
          if (response.code  === '000000') {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 700,
              onClose: () => {
                this.getDataList()
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      })
    }

  },

}
</script>
<style>
.filter-item {
  margin-right: 10px;
}

</style>
